package com.wd.base.util;

/**
 * NA<BR>
 *
 * @author w4425
 * @version [V5.9.3, 2021/6/4]
 * @since V5.9.3
 */
public class TextUtils {
    /**
     * 显示最长的字符个数[中文2个字符 ，英文1个字符 ]
     *
     * @param str
     * @param maxLen
     * @return 字符
     */
    public static String handleText(String str, int maxLen) {
        if (android.text.TextUtils.isEmpty(str)) {
            return str;
        }
        try {
            int count = 0;
            int endIndex = 0;
            for (int i = 0; i < str.length(); i++) {
                char item = str.charAt(i);
                if (item < 128) {
                    count = count + 1;
                } else {
                    count = count + 2;
                }
                if (maxLen == count || (item >= 128 && maxLen + 1 == count)) {
                    endIndex = i;
                }
            }
            if (count <= maxLen) {
                return str;
            } else {
                return str.substring(0, endIndex + 1);
            }
        } catch (Exception e) {
            return str;
        }
    }
}
